package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.turing.post.organization.util.VoucherInStockOrder;
import com.turing.post.organization.util.VoucherInStockOrderDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author wzd
 * @2013-03-12
 */
@Stateless(mappedName = "organization.impl.VoucherInStockOrderDaoImpl")
@Remote
@Local
public class VoucherInStockOrderDaoImpl implements VoucherInStockOrderDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 根据条件查询凭证入库单列表
	 * 
	 * @param issueBranchNo
	 *            发卡商机构编号
	 * @param voucherId
	 *            凭证种类ID
	 * @param startDate
	 *            开始交易日期
	 * @param endDate
	 *            结束交易日期
	 * @param page
	 *            页码
	 * @return 凭证入库单列表
	 */
	@SuppressWarnings("unchecked")
	public List<VoucherInStockOrder> getVoucherInStockOrderList(
			PCPSWebInput webInput, String issueBranchNo, String status,
			String startDate, String endDate, Integer page) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| PCPSUtil.isEmpty(startDate) || PCPSUtil.isEmpty(endDate))
			return null;
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from VoucherInStockOrder t where t.TranTime >=to_date('"
				+ startDate
				+ "','yyyy-mm-dd hh24:mi:ss') and t.TranTime <=to_date('"
				+ endDate + "','yyyy-mm-dd hh24:mi:ss')";
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = sql + " and t.IssueCompanyNo = '" + issueCompanyNo + "'";
		}

		sql += PCPSUtil.getSqlCond(webInput, issueBranchNo, "issueBranchNo");

		if (!PCPSUtil.isEmpty(status)) {
			sql = sql + " and t.status='" + status + "'";
		}
		sql = sql + "order by t.TranTime desc";
		Query query=null;
		if(page==-1)
			query = _em.createNativeQuery(sql, VoucherInStockOrder.class);
		else
		    query = _em.createNativeQuery(sql, VoucherInStockOrder.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	/**
	 * 根据条件查询凭证入库单列表
	 * 
	 * @param issueBranchNo
	 *            发卡商机构编号
	 * @param status
	 *            状态
	 * @param startDate
	 *            开始交易日期
	 * @param endDate
	 *            结束交易日期
	 * @return 凭证入库单记录数
	 */
	@Override
	public BigInteger getVoucherInStockOrderListTotal(PCPSWebInput webInput,
			String issueBranchNo, String status, String startDate,
			String endDate) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return BigInteger.ZERO;
		String sql = "select nvl(count(*),0)  from VoucherInStockOrder t where (?1 is null or t.TranTime>=to_date(?1,'yyyy-mm-dd hh24:mi:ss')) and "
				+ "(?2 is null or t.TranTime <=to_date(?2,'yyyy-mm-dd hh24:mi:ss'))";
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = sql + " and t.IssueCompanyNo = '" + issueCompanyNo + "'";
		}
		sql += PCPSUtil.getSqlCond(webInput, issueBranchNo, "issueBranchNo");
		if (!PCPSUtil.isEmpty(status)) {
			sql = sql + " and t.status='" + status + "'";
		}
		sql = sql + "order by t.TranTime,t.Status desc";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, startDate);
		query.setParameter(2, endDate);
		return new BigInteger(query.getSingleResult().toString());
	}

	/**
	 * 根据凭证入库单id查询凭证入库单明细类对象
	 * 
	 * @param voucherInStockOrderId
	 *            凭证入库单id
	 * @return 凭证入库单对象
	 */
	@Override
	public VoucherInStockOrder getVoucherInStockOrdeVoucherId(
			BigInteger voucherInStockOrderId) {
		return _em.find(VoucherInStockOrder.class, voucherInStockOrderId);
	}

	/**
	 * 新增凭证入库单
	 * 
	 * @param voucherInStockOrder
	 *            凭证入库单
	 * @throws PCPSApplicationException
	 */
	public void insertVoucherInStockOrder(
			VoucherInStockOrder voucherInStockOrder)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, voucherInStockOrder);
	}

	/**
	 * 修改凭证入库单
	 * 
	 * @param voucherInStockOrder
	 *            凭证入库单
	 * @throws PCPSApplicationException
	 */
	public void updateVoucherInStockOrder(
			VoucherInStockOrder voucherInStockOrder)
			throws PCPSApplicationException {
		GeneralDao.update(_em, voucherInStockOrder);
	}

	/**
	 * 修改凭证入库单状态
	 * 
	 * @param voucherInStockOrder
	 *            凭证入库单
	 * @throws PCPSApplicationException
	 */
	public void updateVoucherInStockOrderByStatus(
			VoucherInStockOrder voucherInStockOrder)
			throws PCPSApplicationException {
		GeneralDao.update(_em, voucherInStockOrder);
	}

	@Override
	@SuppressWarnings("unchecked")
	public Integer getVoucherInStockOrderListByStartVoucherNo(
			String startVoucherNo) {
		String sql = "select v.* from voucherinstockorderdetail t,voucherinstockorder v where t.voucherinstockorderid=v.voucherinstockorderid and v.status<>'3' and t.startvoucherno<=to_number('"
				+ startVoucherNo
				+ "') and t.endvoucherno>=to_number('"
				+ startVoucherNo + "')";
		Query query = _em.createNativeQuery(sql, VoucherInStockOrder.class);
		List<VoucherInStockOrder> result = query.getResultList();
		if (result == null || result.size() < 1) {
			return 0;
		}
		return query.getResultList().size();
	}
}
